package message_api

import (
	"ginBolg/global"
	"ginBolg/models"
	"ginBolg/models/res"
	"ginBolg/utils/jwt"
	"github.com/gin-gonic/gin"
)

type MessageRecordRequest struct {
	UserId uint `json:"userId"`
}

func (MessageApi) MessageRecordView(c *gin.Context) {
	var cr MessageRecordRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithError(err, &cr, c)
		return
	}
	_claims, _ := c.Get("claims")
	claims := _claims.(*jwt.CustomClaims)

	var _messageList []models.MessageModel
	var messageList = make([]models.MessageModel, 0)
	global.DB.Order("create_at desc").Find(&_messageList, "send_user_id = ? or rev_user_id = ?", claims.UserId, claims.UserId)

	for _, model := range _messageList {
		if model.RevUserID == cr.UserId || model.SendUserID == cr.UserId {
			messageList = append(messageList, model)
		}
	}
	// todo 点开后就要把未读消息改成已读
	res.OkWithData(messageList, c)
	return
}
